//
//  WbStatusesCell.swift
//  Weibo
//
//  Created by HH on 16/12/5.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

class WbStatusesCell: UITableViewCell {
    
    var topConstraint:Constraint?
    
    var statusesViewModel:WbStatusesViewModel?{
        
        didSet{
            
            originalView.statusesViewModel = statusesViewModel
            retweetView.statuesViewModel = statusesViewModel
            toolBar.statusesViewModel = statusesViewModel
//            print(statusesViewModel?.statuses?.retweeted_status?.text)
            
            //根据有无转发微博判断toolBar的位置
            
            topConstraint?.deactivate()
            
            guard (statusesViewModel?.statuses?.retweeted_status) != nil else {
                retweetView.isHidden = true
                
                toolBar.snp.makeConstraints({ (make) in
                   topConstraint = make.top.equalTo(originalView.snp.bottom).offset(1 / UIScreen.main.scale).constraint
                })
                
                return
            }
            retweetView.isHidden = false
            
            toolBar.snp.makeConstraints { (make) in
               topConstraint = make.top.equalTo(retweetView.snp.bottom).offset(1 / UIScreen.main.scale).constraint
            }
            
        }

    }
    
    //原创微博View
    lazy private var originalView:WbStatusesOriginalView = WbStatusesOriginalView()
    //转发微博
    lazy private var retweetView:WbStatuesRetweetView = WbStatuesRetweetView()
    //工具条
    lazy private var toolBar:WbToolBar = WbToolBar()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        selectionStyle = .none
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
//        originalView.backgroundColor = UIColor.randomColor()
        
        //原创微博
        originalView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(8)
            make.left.right.equalToSuperview()
            
//            make.height.equalTo(120)
        }
        //转发微博
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        //底部工具条
        toolBar.snp.makeConstraints { (make) in
           topConstraint = make.top.equalTo(retweetView.snp.bottom).offset(1 / UIScreen.main.scale).constraint
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(35)
        }
        
        
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}
